package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.DepartmentUtil;
import cn.com.blackboard.DataModule4B2.dao.DepartmentInfoMapper;
import cn.com.blackboard.DataModule4B2.model.DepartmentInfo;
import cn.com.blackboard.DataModule4B2.service.DepartmentService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

@Service("DepartmentService")
public class DepartmentServiceImpl implements DepartmentService{

	private static final Logger logger = LoggerFactory.getLogger(DepartmentServiceImpl.class);
	
	@Autowired
	private DepartmentInfoMapper departmentInfoMapper;

	public int insert(DepartmentInfo DepartmentInfo) {
		// TODO Auto-generated method stub
		return departmentInfoMapper.insert(DepartmentInfo);
	}

	@Override
	public List<DepartmentInfo> getDepartmentInfo() {
		// TODO Auto-generated method stub
		return departmentInfoMapper.selectByCondition(Condition.SearchAll);
	}

	@Override
	public int updateStatus(String departmentId) {	
		return departmentInfoMapper.updateRecordImportedByDepartmentId(departmentId);
	}
	
	@Override
	public List<DepartmentInfo> getDepartmentInfo(String jsonCondition) {
		Condition condition = Condition.ParseCondition(jsonCondition);
		String conditionStr = Condition.RenderConditoin(condition);
		return departmentInfoMapper.selectByCondition(conditionStr);
	}

	@Override
	public String getDepartmentNameById(String departmentId) {
		// TODO Auto-generated method stub
		return departmentInfoMapper.selectByPrimaryKey(departmentId).getDepartmentName();
	}

	@Override
	public List<BatchResultModel> importDepartment(List<DepartmentInfo> departmentInfoLst) {
		logger.info("start to import department");
		int count = 0;
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		for(DepartmentInfo di:departmentInfoLst){	
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(DepartmentInfo.class.getName());
			batchResultModel.setModelClsAttr("departmentId-departmentName");
			batchResultModel.setValue(di.getDepartmentId()+"-"+di.getDepartmentName());
			Status status = DepartmentUtil.importDepartment(di);
			batchResultModel.setStatus(status);
			count += status.isSuccess()?1:0;
			if(status.isSuccess())
				updateStatus(di.getDepartmentId());
			list.add(batchResultModel);
		}
		logger.info("Department need to be imported is "+departmentInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}
	
	@Override
	public List<DepartmentInfo> getDepartmentByName(String departmentName) {
		Condition condition = new Condition();
		condition.setAttrStr("departmentName");
		condition.setOperStr("Equals");
		condition.setValue(departmentName);
		condition.setIsExported("all");
		condition.setIsImported("all");
		return departmentInfoMapper.selectByCondition(Condition.RenderConditoin(condition));
	}

	@Override
	public List<BatchResultModel> batchImportDepartment() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(DepartmentInfo.class.getName());
		List<DepartmentInfo> departmentInfoList = departmentInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importDepartment(departmentInfoList);
		return list;
	}
}
